package movies
{
	import flash.filesystem.File;
	import flash.utils.setTimeout;
	import mymyoux.utils.SCache;
	import org.osflash.signals.Signal;
	
	public class MovieDatabase
	{
		protected var _films:Array;
		protected const EXTENSIONS:String = "extensions";
		protected const FOLDERS:String = "folders";
		protected var folderBrowser:FolderBrowser;
		protected var _movieAdded:Signal;
		
		public function MovieDatabase()
		{
			_movieAdded = new Signal(Movie);

		}
		public function init():void
		{
			_films = new Array();
			if(SCache.has("films",Array))
			{
				//_films = SCache.getVar("films");
			}
			
			folderBrowser = new FolderBrowser();
			folderBrowser.urlFound.add(onNewURL);
			
			if(SCache.has(EXTENSIONS,Array))
			{
				folderBrowser.extensions = SCache.getVar(EXTENSIONS);
			}
			if(SCache.has(FOLDERS,Array))
			{
				folderBrowser.urls = SCache.getVar(FOLDERS);
			}
			SCache.varChanged.add(onVarChanged);
		}
		
		public function get movieAdded():Signal
		{
			return _movieAdded;
		}
		protected function onVarChanged(name:String,value:*,oldValue:*):void
		{
			if(name == EXTENSIONS && value is Array)
			{
				folderBrowser.extensions = value;
			}
			if(name == FOLDERS && value is Array)
			{
				folderBrowser.urls = value;
			}
		}
		protected function onNewURL(url:String):void
		{
			var i:int = 0;
			var f:File = new File(url);
			var m:Movie;
			for(; i<_films.length;i++)
			{
				if((_films[i] as Movie).name >= f.name)
				{
					if((_films[i] as Movie).name == f.name)
					{
						if(!(_films[i] as Movie).containsURL(url))
						{
							(_films[i] as Movie).addURL(url);
							trace("new url : "+(_films[i] as Movie).name);
							SCache.setVar("films",_films);
						}
					}else
					{
						m = new Movie(url);
						_films.splice(i,0,m);
						movieAdded.dispatch(m);
						SCache.setVar("films",_films);
					}
					return;
				}
			}
			m = new Movie(url);
			_films.push(m);
			movieAdded.dispatch(m);
			SCache.setVar("films",_films);
		}
		

	}

}
import mymyoux.utils.SCache;
import movies.Movie;
import flash.net.SharedObject;
import flash.system.ApplicationDomain;
internal class urls{
		protected var _urls:Array;
		public function urls(films:Array)
		{
			ApplicationDomain.currentDomain.getDefinition("movies.Movie");
			new Movie();
			var i:int = 0;
			var movie:Movie = new Movie();
			
			_urls = new Array();
			for(;i<films.length;i++)
			{
			//	flash.net.SharedObject.
			/*	trace(films[i].name);
				trace(films[i].files[0].nativePath);
				trace(films[i].test());
				trace(films[i].getUrls());
				//System.
				trace(films[i] as Movie);
				movie = films[i] as Movie;*/
				//_urls.concat(movie.getUrls());		
			}
		}
		public function add(url:String):Boolean
		{
			if(_urls.indexOf(url)==-1)
			{
				_urls.push(url);
				save();
				return true;
			}
			return false;
		}
		public function rem(url:String):void
		{
			if(_urls.indexOf(url)>-1)
			{
				_urls.splice(_urls.indexOf(url),1);
				save();
			}
			
		}
		protected function save():void
		{
			SCache.setVar("urls",_urls);
		}

}